package com.inoneone.youba.mcht.controller;

import com.inoneone.youba.mcht.dao.po.Merchant;
import com.inoneone.youba.mcht.dto.BaseRsp;
import com.inoneone.youba.mcht.dto.PageRsp;
import com.inoneone.youba.mcht.dto.req.FuelOrderPlusReq;
import com.inoneone.youba.mcht.dto.req.FuelOrderReq;
import com.inoneone.youba.mcht.dto.req.KeyInfoReq;
import com.inoneone.youba.mcht.dto.req.MyCardsReq;
import com.inoneone.youba.mcht.dto.rsp.FuelOrderRsp;
import com.inoneone.youba.mcht.dto.rsp.MyCardsRsp;
import com.inoneone.youba.mcht.enums.Constants;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.exception.AssertUtil;
import com.inoneone.toolkit.exception.RegExp;
import com.inoneone.youba.mcht.service.CardApi;
import com.inoneone.youba.mcht.service.OrderApi;
import com.inoneone.youba.mcht.util.CommonUtil;
import com.inoneone.youba.mcht.util.Constant;
import com.inoneone.youba.mcht.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;


@RestController
@RequestMapping(value = "/order", method = {RequestMethod.POST})
public class OrderController {
	@Autowired
	private OrderApi orderApi;

    @PostMapping(value = "/fuelOrder")
    @ResponseBody
    public Object fuelOrder(HttpSession httpSession, @RequestBody FuelOrderReq fuelOrderReq) {
        //参数检查
        if(!CommonUtil.isNullOrEmpty(fuelOrderReq.getCardId()))
        {
            AssertUtil.isMatch(fuelOrderReq.getCardId(), RegExp.CARD_NO, ErrorCode.CARD);

        }

        if(!CommonUtil.isNullOrEmpty(fuelOrderReq.getTankerId()))
        {
            AssertUtil.isMatch(fuelOrderReq.getTankerId(), RegExp.TANKER_ID, ErrorCode.TANKER_ID);

        }
        if(fuelOrderReq.getBeginTime() != null)
        {
            AssertUtil.isBetween(fuelOrderReq.getBeginTime(), Constants.DATETIME_BEGIN, Constants.DATETIME_END, ErrorCode.DATE_TIME);

        }
        if(fuelOrderReq.getEndTime() != null)
        {
            AssertUtil.isBetween(fuelOrderReq.getEndTime(), Constants.DATETIME_BEGIN, Constants.DATETIME_END, ErrorCode.DATE_TIME);
        }
        fuelOrderReq.validate();

        //获取session
        Merchant merchant = CommonUtil.getSession(httpSession);

        //查询订单
        PageRsp<FuelOrderRsp> pageInfo = orderApi.fuelOrder(fuelOrderReq, merchant.getId());

        //返回
        return new BaseRsp(pageInfo);
    }

    @PostMapping(value = "/fuelOrderPlus")
    @ResponseBody
    public Object fuelOrderPlus(HttpSession httpSession, @RequestBody FuelOrderPlusReq fuelOrderPlusReq) {

        //参数校验
        AssertUtil.isMatch(fuelOrderPlusReq.getMonth(), RegExp.YYYYMM, ErrorCode.DATE);

        //年月
        Date month = DateUtil.parseDate(fuelOrderPlusReq.getMonth(), "yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        FuelOrderReq fuelOrderReq = new FuelOrderReq();
        //开始时间
        fuelOrderReq.setBeginTime(calendar.getTime().getTime());

        //结束时间
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        fuelOrderReq.setEndTime(calendar.getTime().getTime());

        BeanUtils.copyProperties(fuelOrderPlusReq, fuelOrderReq);

        return fuelOrder(httpSession, fuelOrderReq);
    }
}
